package com.rd.sys.servlet.auth;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rd.sys.common.base.CommonConstants;
import com.rd.sys.common.web.session.SessionUtil;
import com.rd.sys.dto.commmon.base.EyeResult;
import com.rd.sys.dto.sys.user.UserInfoDto;
import com.rd.sys.dto.sys.user.UserLoginDto;
import com.rd.sys.service.sys.user.UserLoginService;

/**
 * 跳过客户端鉴权步骤,直接访问所有测评项目页面
 * 
 */
public class DirectAccessProjectServlet extends AuthBaseServlet {
    private static final long serialVersionUID = 4604511398661143991L;

    private UserLoginService userLoginService = (UserLoginService) getService("userLoginServiceImpl");

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String userCode = req.getParameter("code");

            /** 1. 鉴权通过,登录并设置Session */
            UserLoginDto loginInfo = new UserLoginDto();
            loginInfo.setUsercode(userCode);
            UserInfoDto userInfo = userLoginService.loginPortalByAuth(loginInfo);
            SessionUtil.createSession(req, CommonConstants.UserConstats.USER_SESSION_FLAG, userInfo);

            /** 2. 重定向 */
            String redirectUrl = "http://" + req.getHeader("Host") + req.getContextPath() + analysisIndexUrl
                    + "?theSource=" + CommonConstants.UserConstats.USER_THE_CLIENT_SOURCE;
            resp.sendRedirect(redirectUrl);

        } catch (Exception ex) {
            logger.error("", ex);
            EyeResult result = new EyeResult(EyeResult.ResultDefine.AUTH_LOGIN_FAIL);
            sendResponse(result, req, resp);
        }
    }
}
